﻿using PivalSoft.LDMS.Data;
using PivalSoft.LDMS.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PivalSoft.LDMS
{

    public partial class frmAddCase : Form
    {
        public int UserId = 0;
        public int caseId = 0;

        public frmAddCase()
        {
            InitializeComponent();
            FillControls();
        }

        private void FillControls()
        {
            LDMSRepository repository = new LDMSRepository();
            cbmParty.DataSource = repository.GetContactByType(1);
            cmbPartyAdvocate.DataSource = repository.GetContactByType(1);
            cmbOppositionParty.DataSource = repository.GetContactByType(1);
            cmbOppositionAdvocate.DataSource = repository.GetContactByType(1);

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateCase())
            {
                LDMSRepository repository = new LDMSRepository();
                Case courtCase = new Case();
                courtCase.CaseTitle = txtCasetitle.Text.Trim();
                courtCase.CaseDescription = txtCaseDescription.Text.Trim();
                courtCase.PartyID = Convert.ToInt32(cbmParty.SelectedValue);
                courtCase.AdvocateID = Convert.ToInt32(cmbPartyAdvocate.SelectedValue);
                courtCase.CreatedBy = UserId;
                courtCase.CreatedDate = DateTime.Now;
                courtCase.OppositionAdvocateID = Convert.ToInt32(cmbOppositionAdvocate.SelectedValue);
                courtCase.OppositionPartyID = Convert.ToInt32(cmbOppositionParty.SelectedValue);
                courtCase.StatusID = 1; //Convert.ToInt32(cmbStatus.SelectedValue);
                courtCase.CourtID = Convert.ToInt32(cmbCourt.SelectedValue);
                courtCase.CaseID = caseId;
                repository.AddCase(courtCase);
                MessageBox.Show("Case is saved successfully", Resources.MessageCaption);
                ResetControl();
            }
        }

        private void ResetControl()
        {
            txtCasetitle.Text = string.Empty;
            txtCaseDescription.Text = string.Empty;
            cbmParty.SelectedIndex = 0;
            cmbPartyAdvocate.SelectedIndex = 0;
            cmbOppositionAdvocate.SelectedIndex = 0;
            cmbOppositionParty.SelectedIndex = 0;
            cmbCourt.SelectedIndex = 0;
            caseId = 0;
        }

        private bool ValidateCase()
        {
            bool isValid = true;
            if (string.IsNullOrEmpty(txtCasetitle.Text.Trim()))
            {
                MessageBox.Show(Resources.RequiredCaseTitle, Resources.MessageCaption);
                isValid = false;
            }
            if (string.IsNullOrEmpty(txtCaseDescription.Text.Trim()))
            {
                MessageBox.Show(Resources.RequiredCaseDesc, Resources.MessageCaption);
                isValid = false;
            }
            return isValid;
        }

        private void btnPartyMore_Click(object sender, EventArgs e)
        {
            SearchCase();
        }

        private void frmAddCase_Load(object sender, EventArgs e)
        {
            dgCase.AutoGenerateColumns = false;
        }

        private void SearchCase()
        {
            List<KeyValuePair<string, string>> parameters = new List<KeyValuePair<string, string>>();
            if (!string.IsNullOrEmpty(txtSCaseTitle.Text.Trim()))
            {
                parameters.Add(new KeyValuePair<string, string>(Constants.TITLE, txtSCaseTitle.Text));
            }
            if (!string.IsNullOrEmpty(txtSCaseDesc.Text.Trim()))
            {
                parameters.Add(new KeyValuePair<string, string>(Constants.DESC, txtSCaseDesc.Text));
            }
            if (cmbSCourt.SelectedIndex > 0)
            {
                parameters.Add(new KeyValuePair<string, string>(Constants.COURT, cmbSCourt.SelectedValue.ToString()));
            }
            if (cmbSOppAdvocate.SelectedIndex > 0)
            {
                parameters.Add(new KeyValuePair<string, string>(Constants.OPPADVOCATE, cmbSOppAdvocate.SelectedValue.ToString()));
            }
            if (cmbSOppPartyName.SelectedIndex > 0)
            {
                parameters.Add(new KeyValuePair<string, string>(Constants.OPPPARTY, cmbSOppPartyName.SelectedValue.ToString()));
            }
            if (cmbSPartyAdvocate.SelectedIndex > 0)
            {
                parameters.Add(new KeyValuePair<string, string>(Constants.PARTYADVOCATE, cmbSPartyAdvocate.SelectedValue.ToString()));
            }
            if (cmbSPartyName.SelectedIndex > 0)
            {
                parameters.Add(new KeyValuePair<string, string>(Constants.PARTY, cmbSPartyName.SelectedValue.ToString()));
            }

            if (parameters.Count > 0)
            {
                LDMSRepository repository = new LDMSRepository();
                var cases = repository.SearchCase(parameters);
                dgCase.DataSource = cases;
            }
            else
            {
                MessageBox.Show(Resources.NoneFilter, Resources.MessageCaption);
            }
        }

        private void dgCase_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (Convert.ToString(dgCase.Rows[e.RowIndex].Cells[e.ColumnIndex].Value).Equals("Edit"))
            {
                txtCasetitle.Text = Convert.ToString(dgCase.Rows[e.RowIndex].Cells["CaseTitle"].Value);
                txtSCaseDesc.Text = Convert.ToString(dgCase.Rows[e.RowIndex].Cells["CaseDescription"].Value);
                cmbCourt.SelectedIndex = cmbCourt.Items.IndexOf(cmbCourt.Items.Cast<TypeMaster>().FirstOrDefault(s =>
                    s.TypeMasterID == Convert.ToInt32(dgCase.Rows[e.RowIndex].Cells["CourtID"].Value)));
                cmbOppositionAdvocate.SelectedIndex = cmbOppositionAdvocate.Items.IndexOf(cmbOppositionAdvocate.Items.Cast<TypeMaster>().FirstOrDefault(s =>
                   s.TypeMasterID == Convert.ToInt32(dgCase.Rows[e.RowIndex].Cells["OppositionAdvocateID"].Value)));
                cmbOppositionParty.SelectedIndex = cmbOppositionParty.Items.IndexOf(cmbOppositionParty.Items.Cast<TypeMaster>().FirstOrDefault(s =>
                   s.TypeMasterID == Convert.ToInt32(dgCase.Rows[e.RowIndex].Cells["OppositionPartyID"].Value)));
                cmbPartyAdvocate.SelectedIndex = cmbPartyAdvocate.Items.IndexOf(cmbPartyAdvocate.Items.Cast<TypeMaster>().FirstOrDefault(s =>
                   s.TypeMasterID == Convert.ToInt32(dgCase.Rows[e.RowIndex].Cells["AdvocateID"].Value)));
                cbmParty.SelectedIndex = cbmParty.Items.IndexOf(cbmParty.Items.Cast<TypeMaster>().FirstOrDefault(s =>
                   s.TypeMasterID == Convert.ToInt32(dgCase.Rows[e.RowIndex].Cells["PartyID"].Value)));

                caseId = Convert.ToInt32(dgCase.Rows[e.RowIndex].Cells["ColCaseID"].Value);
                dgCase.DataSource = null;
            }
            else if (Convert.ToString(dgCase.Rows[e.RowIndex].Cells[e.ColumnIndex].Value).Equals("AddHearing"))
            {
                frmCaseHearing hearing = new frmCaseHearing();
                hearing.caseId = Convert.ToInt32(dgCase.Rows[e.RowIndex].Cells["ColCaseID"].Value);
                hearing.ShowDialog(this);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
